<?php
/**
 *
 * @package avp
 * @version $Id: $
 * @copyright (c) 2006 Jan-Willem van Ganswijk, Stefan van Essen
 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
 */
define('IN_SCRIPT', 'TRUE'); //Voor de evil haxx0rz.

$root = '../';

include($root . 'includes/common.inc.php');
include($root . 'includes/lang.inc.php');
include($root . 'includes/session.class.php');
include($root . 'includes/template.class.php');
include($root . 'includes/admin.inc.php');

$session = new sessionWrapper();
$lang = read_langfiles('admin_common,admin_settings');
$template = new template($CONFIG['theme']);
$template->add_var('active', 'settings');

if (!$session->check_login()) {
    header('location: login.php');
    die(print_error($lang['ERROR_NEEDLOGIN'], 'login.php', true));
}
if (isset($_GET['submit'])) {
    if (!isset($_POST['userpassword']) || $_POST['userpassword'] == '' || ($_POST['userpassword_repeat'] != $_POST['userpassword'])) {
        $passwordbit = $CONFIG['userpass'];
    } elseif ($_POST['userpassword_repeat'] == $_POST['userpassword']) {
        $passwordbit = md5($_POST['userpassword']) ;
    } else {
        die(print_error($lang['ERROR_PASS_NOMATCH'], $_SERVER['PHP_SELF']));
    }
    $username = addslashes($_POST['username']);
    $langname = addslashes($_POST['langname']);
    if (!is_readable($root . "lang/{$langname}/conf.lang.php")) {
        die(print_error($lang['ERROR_UNKOWN_LANG'], $_SERVER['PHP_SELF']));
    }
    if ($_POST['per_page'] < 1) {
        $_POST['per_page'] = 1;
    }

    $mod_rewrite = (int)$_POST['mod_rewrite'];
    if ($mod_rewrite != 0 && $mod_rewrite != 1) {
        $mod_rewrite = 0;
    }

    $db->query("UPDATE `" . $db->get_table("settings") . "` SET
	`lang`='{$langname}',
	`username`='{$username}',
	`userpass`='{$passwordbit}',
	`not_allowed_image`=" . (int)addslashes($_POST['notallowedimageid']) . ",
	`not_found_image`=" . (int)addslashes($_POST['notfoundimageid']) . ",
	`not_found_dim_width`=" . (int)addslashes($_POST['size_width']) . ",
	`not_found_dim_height`=" . (int)addslashes($_POST['size_height']) . ",
	`thumbnail_size`=" . (int)addslashes($_POST['thumbsize']) . ",
	`theme`='" . addslashes($_POST['theme']) . "',
	`per_page`=" . (int)addslashes($_POST['per_page']) . ",
	`mod_rewrite`=" . $mod_rewrite . ",
	`webpath`='" . addslashes($_POST['webpath']). "'");
    die(print_message($lang['TITLE_SETTINGS'], $lang['MSG_SUCCES'], 'index.php'));
}

$result = $db->query("SELECT `imagename`,`imageid` FROM `" . $db->get_table("images") . "` ORDER BY `imagename`");
$images = array();
while ($imagerow = $db->fetch($result)) {
    $images[$imagerow['imageid']] = $imagerow['imagename'];
}
$db->free_result($result);

$langfiles = array();
if ($dir_handle = @opendir($root . 'lang')) {
    while ($file = readdir($dir_handle)) {
        if ($file == '.' || $file == '..') {
            continue;
        }
        if (is_dir($root . 'lang/' . $file)) {
            $langinfo = get_langdetails($file);
            $langfiles[$langinfo['name_short']] = $langinfo['name'] . ' (' . strtoupper($langinfo['name_short']) . ')';
        }
    }

    closedir($dir_handle);
}

$values['lang'] = $langdirs;

$themes = array();
if ($dir_handle = @opendir($root . 'templates/')) {
    while ($file = readdir($dir_handle)) {
        if ($file == '.' || $file == '..' || $file == 'compiled' || $file == 'configs') {
            continue;
        }
        if (is_readable($root . 'templates/' . $file . '/theme.cfg.php')) {
            $theme = array();
            require($root . 'templates/' . $file . '/theme.cfg.php');
            $themes[$file] = $theme['name'];
        }
    }

    closedir($dir_handle);
}

$mod_rewrite_radios = array(1 => $lang['YES'], 0 => $lang['NO']);

$template->set_title($lang['TITLE_SETTINGS']);

$template->add_vars(array('CONFIG' => $CONFIG, 'images' => $images, 'langfiles' => $langfiles, 'themes' => $themes, 'mod_rewrite_radios' => $mod_rewrite_radios));

$template->add_templates('admin_header,admin_settings,admin_footer');
$template->make_page();

?>